package com.wy2cloud.gateway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.wy2cloud.gateway.feignclients.UserClient;
import com.wy2cloud.jjwt.security.SecurityUserFactory;
import com.wy2cloud.user.entity.SysUser;

@Service
public class SecurityUserService implements UserDetailsService {

	@Autowired
	private UserClient userClient;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = userClient.getUserByAccount(username);

        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return SecurityUserFactory.generator(user.getId(), user.getAccount(), user.getPassword(), null,null);
        }
	}

}
